package com.cloudptt.wp.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.views.listener.BraceSpaceListener;
import com.cloudptt.wp.ui.views.listener.DialListener;


public class DialLayout extends LinearLayout implements OnClickListener,BraceSpaceListener {

	public static final String TAG = "DialLayout";
	private StringBuffer inputTextBuf = new StringBuffer("");
	private ClearEditText numEt = null;
	private ImageView callIv = null;
	private ImageView msgIv = null;

	private DialListener listener = null;

	public DialLayout(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public DialLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public DialLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		// TODO Auto-generated constructor stub
	}

	public DialLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.layout_dial, this);

		numEt = (ClearEditText)findViewById(R.id.numEt);
		numEt.setBraceSpaceListener(this);

		AnimButton btn_1 = (AnimButton)findViewById(R.id.btn_1);
		btn_1.setOnClickListener(this);

		AnimButton btn_2 = (AnimButton)findViewById(R.id.btn_2);
		btn_2.setOnClickListener(this);

		AnimButton btn_3 = (AnimButton)findViewById(R.id.btn_3);
		btn_3.setOnClickListener(this);

		AnimButton btn_4 = (AnimButton)findViewById(R.id.btn_4);
		btn_4.setOnClickListener(this);

		AnimButton btn_5 = (AnimButton)findViewById(R.id.btn_5);
		btn_5.setOnClickListener(this);

		AnimButton btn_6 = (AnimButton)findViewById(R.id.btn_6);
		btn_6.setOnClickListener(this);

		AnimButton btn_7 = (AnimButton)findViewById(R.id.btn_7);
		btn_7.setOnClickListener(this);

		AnimButton btn_8 = (AnimButton)findViewById(R.id.btn_8);
		btn_8.setOnClickListener(this);

		AnimButton btn_9 = (AnimButton)findViewById(R.id.btn_9);
		btn_9.setOnClickListener(this);

		callIv = (ImageView)findViewById(R.id.callIv);
		callIv.setOnClickListener(this);

		AnimButton btn_0 = (AnimButton)findViewById(R.id.btn_0);
		btn_0.setOnClickListener(this);

		msgIv = (ImageView)findViewById(R.id.msgIv);
		msgIv.setOnClickListener(this);

		setEnable(false);
	}

	public void onStop()
	{
		Log.d(TAG, "onStop");
	}
	public void onResume()
	{
		Log.d(TAG, "onResume");
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		Log.debug(DialLayout.class,"onClick view.getId() = " + view.getId());
		String num =null;
		switch (view.getId())
		{
			case R.id.btn_1:
				inputTextBuf = inputTextBuf.append("1");
				break;
			case R.id.btn_2:
				inputTextBuf = inputTextBuf.append("2");
				break;
			case R.id.btn_3:
				inputTextBuf = inputTextBuf.append("3");
				break;
			case R.id.btn_4:
				inputTextBuf = inputTextBuf.append("4");
				break;
			case R.id.btn_5:
				inputTextBuf = inputTextBuf.append("5");
				break;
			case R.id.btn_6:
				inputTextBuf = inputTextBuf.append("6");
				break;
			case R.id.btn_7:
				inputTextBuf = inputTextBuf.append("7");
				break;
			case R.id.btn_8:
				inputTextBuf = inputTextBuf.append("8");
				break;
			case R.id.btn_9:
				inputTextBuf = inputTextBuf.append("9");
				break;
			case R.id.callIv:
				num = inputTextBuf.toString();
				if(listener != null
					&& num != null
					&& num.length() > 0)
				{
					listener.onFunc(DialListener.DialFunc_Call,num);
					inputTextBuf.delete(0,num.length());
				}
				break;
			case R.id.btn_0:
				inputTextBuf = inputTextBuf.append("0");
				break;
			case R.id.msgIv:
				num = inputTextBuf.toString();
				if(listener != null
						&& num != null
						&& num.length() > 0)
				{
					listener.onFunc(DialListener.DialFunc_Msg,num);
					inputTextBuf.delete(0,num.length());
				}
				break;
		}
		num = inputTextBuf.toString();
		Log.debug(DialLayout.class,"onClick num = " + num);
		numEt.setText(num);
		if(num != null
			&& num.length() > 0)
		{
			setEnable(true);
		}
		else
		{
			setEnable(false);
		}
	}

	public String getNumStr()
	{
		if(numEt != null) {
			return numEt.getText().toString();
		}
		return "";
	}

	private void setEnable(boolean isEnable)
	{
		if(isEnable)
		{
			callIv.setImageResource(R.drawable.btn_call_normal);
			callIv.setEnabled(true);
			msgIv.setImageResource(R.drawable.btn_msg_normal);
			msgIv.setEnabled(true);
		}
		else
		{
			callIv.setImageResource(R.drawable.btn_call_pressed);
			callIv.setEnabled(false);
			msgIv.setImageResource(R.drawable.btn_msg_pressed);
			msgIv.setEnabled(false);
		}
	}

	public void clear()
	{
		if(inputTextBuf != null)
		{
			inputTextBuf.setLength(0);
		}
		numEt.setText("");
	}

	@Override
	public void brackSpace() {
		int len = inputTextBuf.length();
		if(len > 0) {
			this.inputTextBuf.delete(len - 1, len);
		}
		String num = inputTextBuf.toString();
		Log.debug(DialLayout.class,"brackSpace num = " + num);
		numEt.setText(num);
		if(num != null
				&& num.length() > 0)
		{
			setEnable(true);
		}
		else
		{
			setEnable(false);
		}
	}

	public void setDialListener(DialListener listener) {
		this.listener = listener;
	}
}
